<template>
	<view class="vote-page">
		<!-- 标签页 -->
		<view>
			<u-sticky bgColor="#2C57F6">
				<u-tabs :list="stickyList" :current="currentTab" :itemStyle="{height:'70px',width:'75px'}"
					:activeStyle="{color:'#FFFFFF'}" @click="handleTabClick" :inactiveStyle="{color:'#000000'}"
					lineWidth="30px"></u-tabs>
			</u-sticky>
		</view>
		

		<!-- 校区介绍区域 -->
		<view class="campus-intro">
			<view class="intro-title">
				<text>校区名称：{{campus.campusName}}</text>
			</view>
			<view class="intro-desc">

				<text>{{campus.campusIntroduction}}</text>
			</view>
		</view>

		<!-- 投票活动列表区域 -->
		<view class="vote-list-container">
			<view class="vote-list-title">
				<text>当前投票活动</text>
			</view>

			<!-- 投票活动列表项 -->
			<view class="vote-item" v-for="(item, index) in voteList" :key="index" @click="goToVoteDetail(item.voteId)">
				<view class="vote-status" v-if="item.exhStatus === 1">进行中</view>
				<view class="vote-status" v-if="item.exhStatus === 2">已结束</view>
				<view class="vote-status" v-if="item.exhStatus === 0">未开始</view>

				<view class="vote-info">
					<view class="vote-name">{{ item.exhTitle }}</view>
					<view class="vote-time">
						<text>开始时间：{{ item.exhVoteStart }}</text>
						<text>结束时间：{{ item.exhVoteEnd }}</text>
					</view>
				</view>

				<view class="vote-action">
					<view class="join-btn" :class="{'active': item.exhStatus === 1, 'green-btn': item.exhStatus === 0}"
						:disabled="item.exhStatus !== 1" @click.stop="joinVote(item.exhId)">
						<text>{{ item.exhStatus === 1 ? '参与投票' : item.exhStatus === 2 ? '已结束' : '未开始' }}</text>
					</view>
				</view>
			</view>

			<!-- 空状态提示 -->
			<view class="empty-tip" v-if="voteList.length === 0">
				<u-icon name="empty" size="100rpx" color="#999999"></u-icon>
				<text>暂无投票活动</text>
			</view>
		</view>

		<!-- 底部提示 -->
		<view class="vote-tip">
			<text>请关注校园动态，参与精彩投票活动</text>
		</view>
	</view>
</template>

<script>
	import { mapGetters, mapActions } from 'vuex';
	import {
		queryCampusInfo,
		queryCampusVoteInfo
	} from '@/api/vote.js'
	export default {
		data() {
			return {
				user: {
					campusId: 1,
				},
				currentTab: 1,
				stickyList: [{
						name: '校区作品'
					},
					{
						name: '投票'
					},
					{
						name: '艺术家相册'
					},
					{
						name: '社区'
					},
				],
				campus: [], //校区信息
				voteList: []
			}
		},
		onShow() {
			this.handleQueryCampusInfo();
			this.handleQueryCampusVoteInfo();
			this.currentTab = this.activeTab;
		},
		created() {
			this.handleQueryCampusInfo();
			this.handleQueryCampusVoteInfo();
		},
		watch: {
		  activeTab(newVal) {
		    this.currentTab = newVal;
		  }
		},
		computed: {
			...mapGetters(['getActiveTab']),
		
			// 简化访问
			activeTab() {
				return this.getActiveTab;
			}
		},
		methods: {
			...mapActions(['updateActiveTab']),
			handleQueryCampusVoteInfo() {
				const data = {
					campusId: this.user.campusId,
				};
				queryCampusVoteInfo(data).then(res => {
					this.voteList=res.data.voteList;
					console.log(res);
				}).catch(err => {
					console.log("查询校区列表有误");
				})
			},
			//查询校区信息
			handleQueryCampusInfo() {
				const data = {
					campusId: this.user.campusId,
				};
				queryCampusInfo(data).then(res => {
					this.campus = res.data.campus;
					console.log(this.campus);
				}).catch(err => {
					console.log("查询校区列表有误");
				})
			},
			// 返回上一页
			goBack() {
				uni.navigateBack();
			},

			// 前往投票详情页
			goToVoteDetail(voteId) {
				uni.navigateTo({
					url: `/pages/vote-detail/index?voteId=${voteId}`
				});
			},

			// 参与投票
			joinVote(exhId) {
			    // 查找对应的投票活动
			    const voteItem = this.voteList.find(item => item.exhId === exhId);
			    
			    if (!voteItem) {
			        uni.showToast({
			            title: '投票活动不存在',
			            icon: 'none'
			        });
			        return;
			    }
			    
			    if (voteItem.exhStatus !== 1) {
			        uni.showToast({
			            title: voteItem.exhStatus === 0 ? '活动未开始，暂不可参与' : '活动已结束',
			            icon: 'none'
			        });
			        return;
			    }
			    
			    uni.showToast({
			        title: '正在前往投票...',
			        icon: 'loading'
			    });
			    wx.setStorageSync("exhId",exhId)
			    setTimeout(() => {
			        uni.navigateTo({
			            url: `/pages/vote/vote`
			        });
			    }, 1000);
			},
			// 标签跳转方法
			handleTabClick(e) {
				const tabIndex = e.index;
				this.updateActiveTab(tabIndex); // 更新 Vuex 状态
			
				if (tabIndex === 2) { // 点击"投票"标签
					console.log("跳转艺术家相册");
					uni.navigateTo({
						url: '/pages/art-album/art-album'
					});
				} else if (tabIndex === 0) {
					console.log("跳转校区作品");
					uni.switchTab({
						url: '/pages/art-gallery/art-gallery'
					});
				}else if(tabIndex === 3){
					console.log("跳转社区");
					uni.navigateTo({
						url: '/pages/community/community'
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.vote-page {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #F5F7FA;

		.vote-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;
			height: 120rpx;
			background-color: #2C57F6;
			color: #FFFFFF;
			font-size: 36rpx;
			font-weight: bold;

			.header-left,
			.header-right {
				width: 80rpx;
				height: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}

		.campus-intro {
			background-color: #FFFFFF;
			border-radius: 16rpx;
			padding: 30rpx;
			margin: 20rpx 30rpx;

			.intro-title {
				font-size: 32rpx;
				color: #333333;
				font-weight: 500;
				margin-bottom: 10rpx;
			}

			.intro-desc {
				font-size: 26rpx;
				color: #666666;
				line-height: 1.6;
			}
		}

		.vote-list-container {
			flex: 1;
			padding: 0 30rpx;
			box-sizing: border-box;

			.vote-list-title {
				font-size: 32rpx;
				color: #333333;
				font-weight: 500;
				margin: 20rpx 0 16rpx;
			}

			.vote-item {
				background-color: #FFFFFF;
				border-radius: 16rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

				.vote-status {
					position: absolute;
					top: 20rpx;
					right: 20rpx;
					padding: 4rpx 16rpx;
					font-size: 24rpx;
					border-radius: 30rpx;
					font-weight: 500;
				}

				.vote-status[status="1"] {
					background-color: #2C57F6;
					/* 进行中：蓝色 */
					color: #FFFFFF;
				}

				.vote-status[status="0"] {
					background-color: #00B42A;
					/* 未开始：绿色标签 */
					color: #FFFFFF;
				}

				.vote-status[status="2"] {
					background-color: #999999;
					/* 已结束：灰色 */
					color: #FFFFFF;
				}

				.vote-info {
					flex: 1;
					margin-right: 20rpx;

					.vote-name {
						font-size: 30rpx;
						color: #333333;
						font-weight: 500;
						margin-bottom: 12rpx;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						overflow: hidden;
					}

					.vote-time {
						font-size: 24rpx;
						color: #999999;
						line-height: 1.8;
						display: flex;
						flex-direction: column;
						/* 时间上下排列 */
					}

					.vote-time text {
						margin-bottom: 4rpx;
						/* 时间行间距 */
					}
				}

				.vote-action {
					display: flex;
					align-items: center;

					.join-btn {
						width: 160rpx;
						height: 60rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						border-radius: 30rpx;
						font-size: 28rpx;
						transition: all 0.3s;
						border: 1rpx solid #DDDDDD;
					}

					.join-btn.active {
						background-color: #2C57F6;
						/* 进行中：蓝色按钮 */
						color: #FFFFFF;
						border-color: #2C57F6;
					}

					.join-btn.green-btn {
						background-color: #00B42A;
						/* 未开始：绿色按钮 */
						color: #FFFFFF;
						border-color: #00B42A;
					}

					.join-btn:not(.active):not(.green-btn) {
						background-color: #F5F7FA;
						/* 已结束：灰色按钮 */
						color: #999999;
						border-color: #DDDDDD;
					}
				}
			}

			.empty-tip {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				color: #999999;

				.u-icon {
					margin-bottom: 20rpx;
				}

				text {
					font-size: 28rpx;
				}
			}
		}

		.vote-tip {
			padding: 20rpx 30rpx;
			font-size: 24rpx;
			color: #999999;
			text-align: center;
		}
	}
</style>